home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / wnos / wn941101 / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-10  |  3.9 KB  |  216 lines

  1. /* Miscellaneous machine independent utilities */
  2. #include <stdio.h>
  3. #include "global.h"
  4. #include "socket.h"
  5. #include "mbuf.h"
  6.  
  7. /* Select from an array of strings, or return ascii number if out of range */
  8. char *
  9. smsg(msgs,nmsgs,n)
  10. char *msgs[];
  11. unsigned nmsgs,n;
  12. {
  13.     static char buf[20];
  14.  
  15.     if(n < nmsgs && msgs[n] != NULLCHAR)
  16.         return msgs[n];
  17.  
  18.     sprintf(buf,"%u",n);
  19.  
  20.     return buf;
  21. }
  22.  
  23. /* Convert hex-ascii to integer */
  24. int
  25. htoi(s)
  26. char *s;
  27. {
  28.     int i = 0;
  29.     char c;
  30.  
  31.     while((c = *s++) != '\0'){
  32.         if(c == 'x')
  33.             continue;    /* allow 0x notation */
  34.         if('0' <= c && c <= '9')
  35.             i = (i * 16) + (c - '0');
  36.         else if('a' <= c && c <= 'f')
  37.             i = (i * 16) + (c - 'a' + 10);
  38.         else if('A' <= c && c <= 'F')
  39.             i = (i * 16) + (c - 'A' + 10);
  40.         else
  41.             break;
  42.     }
  43.     return i;
  44. }
  45.  
  46. /* replace terminating end of line marker with null */
  47. void
  48. rip(s)
  49. char *s;
  50. {
  51.     char *cp;
  52.  
  53.     if((cp = strchr(s,'\n')) != NULLCHAR)
  54.         *cp = '\0';
  55. }
  56.  
  57. /* Copy a string to a malloc'ed buffer. Turbo C has this one in its
  58.  * library, but it doesn't call mallocw() and can therefore return NULL.
  59.  * NOS uses of strdup() generally don't check for NULL, so they need this one.
  60.  */
  61. /* strdup function now in alloc.c and renamed to strxdup - DB3FL/DK5DC */
  62.  
  63. /* Routines not needed for Turbo 2.0, but available for older libraries */
  64. #ifdef AZTEC
  65. /* Case-insensitive string comparison */
  66. strnicmp(a,b,n)
  67. char *a,*b;
  68. int n;
  69. {
  70.     char a1,b1;
  71.  
  72.     while(n-- != 0 && (a1 = *a++) != '\0' && (b1 = *b++) != '\0'){
  73.         if(a1 == b1)
  74.             continue;    /* No need to convert */
  75.         a1 = tolower(a1);
  76.         b1 = tolower(b1);
  77.         if(a1 == b1)
  78.             continue;    /* NOW they match! */
  79.         if(a1 > b1)
  80.             return 1;
  81.         if(a1 < b1)
  82.             return -1;
  83.     }
  84.     return 0;
  85. }
  86.  
  87. char *
  88. strtok(s1,s2)
  89. char *s1;    /* Source string (first call) or NULL */
  90. #ifdef    __STDC__    /* Ugly kludge for aztec's declaration */
  91. const char *s2;    /* Delimiter string */
  92. #else
  93. char *s2;    /* Delimiter string */
  94. #endif
  95. {
  96.     static int isdelim();
  97.     static char *next;
  98.     char *cp, *tmp;
  99.  
  100.     if(s2 == NULLCHAR)
  101.         return NULLCHAR;    /* Must give delimiter string */
  102.  
  103.     if(s1 != NULLCHAR)
  104.         next = s1;        /* First call */
  105.  
  106.     if(next == NULLCHAR)
  107.         return NULLCHAR;    /* No more */
  108.  
  109.     /* Find beginning of this token */
  110.     for(cp = next;*cp != '\0' && isdelim(*cp,s2);cp++)
  111.         ;
  112.  
  113.     if(*cp == '\0')
  114.         return NULLCHAR;    /* Trailing delimiters, no token */
  115.  
  116.     /* Save the beginning of this token, and find its end */
  117.     tmp = cp;
  118.     next = NULLCHAR;    /* In case we don't find another delim */
  119.     for(;*cp != '\0';cp++){
  120.         if(isdelim(*cp,s2)){
  121.             *cp = '\0';
  122.             next = cp + 1;    /* Next call will begin here */
  123.             break;
  124.         }
  125.     }
  126.     return tmp;
  127. }
  128. static int
  129. isdelim(c,delim)
  130. char c;
  131. register char *delim;
  132. {
  133.     char d;
  134.  
  135.     while((d = *delim++) != '\0'){
  136.         if(c == d)
  137.             return 1;
  138.     }
  139.     return 0;
  140. }
  141. #endif    /* AZTEC */
  142.  
  143.  
  144.  
  145. /* Host-network conversion routines, replaced on the 8086 with assembler */
  146. #ifndef    MSDOS
  147. /* Put a long in host order into a char array in network order */
  148. char *
  149. put32(cp,x)
  150. register char *cp;
  151. int32 x;
  152. {
  153.     *cp++ = x >> 24;
  154.     *cp++ = x >> 16;
  155.     *cp++ = x >> 8;
  156.     *cp++ = x;
  157.     return cp;
  158. }
  159. /* Put a short in host order into a char array in network order */
  160. char *
  161. put16(cp,x)
  162. register char *cp;
  163. int16 x;
  164. {
  165.     *cp++ = x >> 8;
  166.     *cp++ = x;
  167.  
  168.     return cp;
  169. }
  170. int16
  171. get16(cp)
  172. register char *cp;
  173. {
  174.     register int16 x;
  175.  
  176.     x = uchar(*cp++);
  177.     x <<= 8;
  178.     x |= uchar(*cp);
  179.  
  180.     return x;
  181. }
  182. /* Machine-independent, alignment insensitive network-to-host long conversion */
  183. int32
  184. get32(cp)
  185. register char *cp;
  186. {
  187.     register int32 rval;
  188.  
  189.     rval = uchar(*cp++);
  190.     rval <<= 8;
  191.     rval |= uchar(*cp++);
  192.     rval <<= 8;
  193.     rval |= uchar(*cp++);
  194.     rval <<= 8;
  195.     rval |= uchar(*cp);
  196.  
  197.     return rval;
  198. }
  199. /* Compute int(log2(x)) */
  200. int
  201. log2(x)
  202. register int16 x;
  203. {
  204.     register int n = 16;
  205.     for(;n != 0;n--){
  206.         if(x & 0x8000)
  207.             break;
  208.         x <<= 1;
  209.     }
  210.     n--;
  211.     return n;
  212. }
  213.  
  214. #endif
  215.  
  216.